package com.jn.glide.transformation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

public class VintageFilterTransformation extends BitmapTransformation {
    private String watermarkText;

    public VintageFilterTransformation() {
        super();

    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {

        Bitmap.Config originalConfig = toTransform.getConfig();

        // 创建一个新的Bitmap，其颜色格式与原始Bitmap相同
        Bitmap source = toTransform.copy(originalConfig, true);
        // 创建颜色矩阵，调整RGB比例实现复古效果
        ColorMatrix matrix = new ColorMatrix();
        // 降低蓝色通道，增强红色和绿色通道
        matrix.set(new float[]{
                0.627f, 0.320f, 0.050f, 0, 0,  // 红色通道
                0.098f, 0.914f, 0.008f, 0, 0,  // 绿色通道
                0.213f, 0.315f, 0.472f, 0, 0,  // 蓝色通道
                0, 0, 0, 1, 0   // 透明度
        });

        // 应用颜色矩阵
        Bitmap result = Bitmap.createBitmap(
                source.getWidth(),
                source.getHeight(),
                source.getConfig()
        );
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(matrix));
        canvas.drawBitmap(source, 0, 0, paint);

        if (source != result) {
            source.recycle();
        }
        return result;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }

    @Override
    public boolean equals(Object o) {
        return false;
    }

    @Override
    public int hashCode() {
        return 0;
    }
}